/**
 * validate 通用校验规则
 */
(function($) {
    if(!$ || !$.validator || typeof $.validator.addMethod != "function") {
        return;
    }

    //过滤空字符，空字符返回false
    $.validator.addMethod("emptyString", function (value, _this, param) {
        return !!$.trim(value);
    });

    //非负数 value>=0时返回true
    $.validator.addMethod("nonnegative", function (value) {
        var isNull = !$.trim(value);
        var isNumber = typeof value === "number";
        var isNonnegative = value >= 0;
        return isNull ? true : isNumber && isNonnegative;
    });

    //校验身份证号码格式
    $.validator.addMethod("idNumber", function (value) {
        console.log("idNumber", this)
        var isNull = !$.trim(value);
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        return isNull ? true : reg.test(value);
    });

    //为负数时校验不通过
    $.validator.addMethod("nonnegative", function (value) {
        var isNull = !$.trim(value);
        var isNumber = typeof Number(value) === "number";
        var isNonnegative = Number(value) >= 0;
        return isNull ? true : isNumber && isNonnegative;
    });

    //开始时间必须小于结束时间(不校验日期的格式)
    //param.large_selector、param.less_selector其中一个的value为空时返回true,
    //param.large_selector > param.less_selector的日期时返回true
    $.validator.addMethod("dateComparator", function (value, _this, param) {
        console.log("dateComparator", this)
        var largeDate = $(param.large_selector).val();
        var lessDate = $(param.less_selector).val();
        var isNull = !$.trim(largeDate) || !$.trim(lessDate);
        var largeTimestamp = new Date(largeDate).getTime();
        var lessTimestamp = new Date(lessDate).getTime();
        console.log("largeData:",largeDate, "lessDate:",lessDate)
        return isNull ? true : largeTimestamp > lessTimestamp;
    });
	
	//校验金额格式是否正确(最多2为小数)
	//true:[0, 0.0, 0.00, 10.00...], false:[000, 0., 010, 001.00...]
	$.validator.addMethod("money", function (value) {
		var isNull = !$.trim(value);
		var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
		return isNull ? true : reg.test(value);
	});

    //简单的11位手机号码校验
    $.validator.addMethod("mobilephone", function (value) {
        var isNull = !$.trim(value);
        var reg = /^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$/;
        return isNull ? true : reg.test(value);
    });
})(window.jQuery);
